package com.mic.yzzn.plc.common.ins.datastructure;

import java.io.Serializable;

/**
 * Created by ASH on 16/3/7.
 */
public class PLCDataRegisterNumber implements Serializable
{
    /** 序列化版本号 */
	private static final long serialVersionUID = -4032821545525580328L;
	/** 起始地址BCD码 */
	private String beginBCDDataNumber = "00000";
	/** 结束地址BCD码 */
    private String endBCDDataNumber = "99999";

    public PLCDataRegisterNumber()
    {
//        setBeginBCDDataNumber("00000");
//        setEndBCDDataNumber("99999");
    }

    public PLCDataRegisterNumber(String beginBCDNumber, String endBCDNumber) throws IllegalArgumentException
    {
        setBeginBCDDataNumber(beginBCDNumber);
        setEndBCDDataNumber(endBCDNumber);
    }

    public String getBeginBCDDataNumber()
    {
        return beginBCDDataNumber;
    }

    public String getEndBCDDataNumber()
    {
        return endBCDDataNumber;
    }

    public void setBeginBCDDataNumber(String beginBCDDataNumber) throws IllegalArgumentException
    {
        // 检测参数范围
        int beginValue = Integer.parseInt(beginBCDDataNumber);
        int endValue = Integer.parseInt(this.endBCDDataNumber);
        if (beginValue < 0 || beginValue > 99999)
        {
            throw new IllegalArgumentException("BCDCode is out of range(0 ~ 99999)");
        }

        if (beginValue > endValue)
        {
            throw new IllegalArgumentException("endBCDDataNumber must less than beginBCDDataNumber");
        }

        // 若不足五位则自动补足
        StringBuffer prefixBuffer = new StringBuffer();
        for (int i = beginBCDDataNumber.length(); i < 5; ++i)
        {
            prefixBuffer.append("0");
        }
        prefixBuffer.append(beginBCDDataNumber);

        this.beginBCDDataNumber = prefixBuffer.toString();
    }

    public void setEndBCDDataNumber(String endBCDDataNumber) throws IllegalArgumentException
    {
        // 检测参数范围
        int endValue = Integer.parseInt(endBCDDataNumber);
        int beginValue = Integer.parseInt(this.beginBCDDataNumber);
        if (endValue < 0 || endValue > 99999)
        {
            throw new IllegalArgumentException("BCDCode is out of range(0 ~ 99999)");
        }

        if (endValue < beginValue)
        {
            throw new IllegalArgumentException("endBCDDataNumber must less than beginBCDDataNumber");
        }

        // 若不足五位则自动补足
        StringBuffer prefixBuffer = new StringBuffer();
        for (int i = endBCDDataNumber.length(); i < 5; ++i)
        {
            prefixBuffer.append("0");
        }
        prefixBuffer.append(endBCDDataNumber);

        this.endBCDDataNumber = prefixBuffer.toString();
    }

    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append(String.valueOf(this.beginBCDDataNumber));
        buffer.append(this.endBCDDataNumber);

        return buffer.toString();
    }

    /**
     * 计算自己每一位的异或结果并返回
     */
    public int getXORByBit()
    {
        int result = 0;
        for (int i = 0; i < beginBCDDataNumber.length(); ++i)
        {

            result ^= beginBCDDataNumber.charAt(i);
        }

        for (int i = 0; i < endBCDDataNumber.length(); ++i)
        {
            result ^= endBCDDataNumber.charAt(i);
        }

        return result;
    }

    public int range()
    {
        int endValue = Integer.parseInt(endBCDDataNumber);
        int beginValue = Integer.parseInt(beginBCDDataNumber);

        return endValue - beginValue + 1;
    }
}
